/**
 * 年月日格式化，年月日小时分钟秒 截取为年月日
 * @param value
 * @returns {string|*}
 */
 export const splitDate = value => {
    if (value) {
      return value.substring(0, 10);
    }
    return value;
  };

  /**
 * 年月日格式化，年月日小时分钟秒 截取为年月日
 * @param value
 * @returns {string|*}
 */
 export const splitDateTime = value => {
    if (value) {
      return value.substring(0, 19);
    }
    return value;
  };
  export const yearDate = value => {
    let date = new Date(value);
  
    const year = date.getFullYear();
    const month =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1;
    const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  
    return year + "-" + month + "-" + day;
  };
  /**
   * 给指定时间增加1年
   * @param value:日期
   * @returns {string|*}
   */
  export const addYearDate = value => {
    let date = new Date(value);
    date.setFullYear(date.getFullYear() + 1);
  
    const year = date.getFullYear().toString();
    const month =
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1;
    const day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  
    return year + "-" + month + "-" + day;
  };
  
  /**
   * 根据生日计算年龄
   * @param value
   * @returns {string|number}
   */
  export const toAge = value => {
    if (!value) {
      return "";
    }
    //出生时间 毫秒
    const birthDayTime = new Date(value).getTime();
    //当前时间 毫秒
    const nowTime = new Date().getTime();
    //一年毫秒数(365 * 86400000 = 31536000000)
    return Math.ceil((nowTime - birthDayTime) / 31536000000);
  };
  
  export const parseTime = value => {
    if (!value) {
      return "";
    }
    const date = new Date(value);
    return `${date.getFullYear()}-${appendZero(date.getMonth() + 1)}-${appendZero(
      date.getDate()
    )} ${appendZero(date.getHours())}:${appendZero(date.getMinutes())}:${appendZero(date.getSeconds())}`;
  };
  
  export const parseDate = value => {
    if (!value) {
      return "";
    }
    const date = new Date(value);
    return `${date.getFullYear()}-${appendZero(date.getMonth() + 1)}-${appendZero(
      date.getDate()
    )}`;
  };
  
  export const parseUnitTime = value => {
    if (!value) {
      return "";
    }
    let minutes = value / 1000 / 60;
    let minutesRound = Math.floor(minutes);
    let seconds = Math.floor(value / 1000 - (60 * minutesRound));
    return `${minutesRound}分${seconds}秒`;
  };
  
  function appendZero(i) {
    if (i < 10) {
      i = "0" + i;
    }
    return i;
  }

  // 转换成两位小数，不足则补充0
  export const towFixNumber = num => {
    if (!num) {
      return "";
    }
    // 判断是否为数字类型   数字类型自动  舍0
    if(typeof(num) == 'number'){
      // 先转成  字符串类型
      num = num.toString()
    }
    // 判断 有无小数点  0 表示 有小数点  -1  表示没有小数点
    if(num.indexOf(".") !== -1){
      // 根据小数点  转换字符串为数组
      let fixArray = num.split('.')
      // 判断 有几位小数  如果有一位  加一个0
      if(fixArray[1].length === 1){
        fixArray = fixArray.join('.')
        fixArray += '0'
        num = fixArray
      }
    }else {
      num += '.00'
    }
    return num;
  }

  export const disableTips = (messageBox) => {
    return new Promise((resolve) => {
        messageBox('是否禁用本条信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        type: 'warning'
        }).then(() => {
        resolve(true);
        }).catch(() => {
        resolve(false);
        });
    })
  }

  export const commandTips = (messageBox) => {
    return new Promise((resolve) => {
        messageBox('是否取消推荐本条信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        type: 'warning'
        }).then(() => {
        resolve(true);
        }).catch(() => {
        resolve(false);
        });
    })
  }

  export const disableCommentsTips = (messageBox) => {
    return new Promise((resolve) => {
        messageBox('删除后，此评论的回复内容也会一并删除，继续吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        type: 'warning'
        }).then(() => {
        resolve(true);
        }).catch(() => {
        resolve(false);
        });
    })
  }

  export const tableHeight = () => {
    const mainBoxHeight = document.querySelector('.mainwarp').clientHeight;
    const searchHeight = document.querySelector('.searchFormBox').clientHeight;
    const paginationHeight = document.querySelector('.el-pagination').clientHeight;
    return mainBoxHeight - searchHeight - 30 - paginationHeight;
  }

  export const contentHeight = () => {
    const mainBoxHeight = document.querySelector('.content-detail').clientHeight;
    return mainBoxHeight;
  }

  export const treeHeight = () => {
    const mainBoxHeight = document.querySelector('.mainwarp').clientHeight;
    const searchHeight = document.querySelector('.searchFormBox').clientHeight;
    return mainBoxHeight - searchHeight - 30;
  }
  export const HTTPIsBoolean = (values) => {
    let str = true;
      const reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
      let url = values.match(reg);
      if (!url) {
        str = false;
      }
      return str;
  }